package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1583 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
            int[][] order = new int[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n - 1; j++) {
                    order[i][preferences[i][j]] = j;
                }
            }
            int[] match = new int[n];
            for (int[] pair : pairs) {
                int person0 = pair[0], person1 = pair[1];
                match[person0] = person1;
                match[person1] = person0;
            }
            int unhappyCount = 0;
            for (int x = 0; x < n; x++) {
                int y = match[x];
                int index = order[x][y];
                for (int i = 0; i < index; i++) {
                    int u = preferences[x][i];
                    int v = match[u];
                    if (order[u][x] < order[u][v]) {
                        unhappyCount++;
                        break;
                    }
                }
            }
            return unhappyCount;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC1583 an = new LC1583();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
